package cn.com.my.gallery.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.com.my.gallery.ImageBean;

public class ImageDatabaseHelper extends SQLiteOpenHelper implements Serializable {

    private static final String DATABASE_NAME = "ImageDatabase.db";
    private static final int DATABASE_VERSION = 1;

    private static final String TABLE_NAME = "images";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_NAME = "name";
    private static final String COLUMN_SIZE = "size";
    private static final String COLUMN_DATA = "data";
    private static final String COLUMN_BUCKET = "bucket";

    private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
            + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + COLUMN_NAME + " TEXT NOT NULL,"
            + COLUMN_SIZE + " INTEGER NOT NULL,"
            + COLUMN_DATA + " TEXT NOT NULL,"
            + COLUMN_BUCKET + " TEXT NOT NULL" + ")";

    public ImageDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    // 插入数据
    public void insertImage(ImageBean image) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(COLUMN_NAME, image.name);
        values.put(COLUMN_SIZE, image.size);
        values.put(COLUMN_DATA, image.data);
        values.put(COLUMN_BUCKET, image.bucket);

        db.insert(TABLE_NAME, null, values);
        db.close();
    }

    // 查询所有图片
    public List<ImageBean> getAllImages() {
        List<ImageBean> imageList = new ArrayList<>();
        String query = "SELECT * FROM " + TABLE_NAME;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(query, null);

        if (cursor.moveToFirst()) {
            do {
                ImageBean image = new ImageBean();
                image.id = cursor.getLong(cursor.getColumnIndex(COLUMN_ID));
                image.name = cursor.getString(cursor.getColumnIndex(COLUMN_NAME));
                image.size = cursor.getInt(cursor.getColumnIndex(COLUMN_SIZE));
                image.data = cursor.getString(cursor.getColumnIndex(COLUMN_DATA));
                image.bucket = cursor.getString(cursor.getColumnIndex(COLUMN_BUCKET));
                imageList.add(image);
            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();

        return imageList;
    }

    // 更新数据
    public void updateImage(ImageBean image) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(COLUMN_NAME, image.name);
        values.put(COLUMN_SIZE, image.size);
        values.put(COLUMN_DATA, image.data);
        values.put(COLUMN_BUCKET, image.bucket);

        db.update(TABLE_NAME, values, COLUMN_ID + " = ?", new String[]{String.valueOf(image.id)});
        db.close();
    }

    // 删除数据
    public void deleteImage(long id) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_NAME, COLUMN_ID + " = ?", new String[]{String.valueOf(id)});
        db.close();
    }
}
